package cn.wizzer.app.web.commons.ext.websocket.action.msg.group;

import cn.wizzer.app.user.modules.models.enums.MsgStatusEnum;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.GroupLogService;
import cn.wizzer.app.user.modules.services.GroupLogUserRelationService;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.param.Param;
import cn.wizzer.framework.util.param.Param4Map;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

@IocBean
@NettyAction(uri = "/group/read",filters = {TokenFilter.class})
public class GroupReadAction extends BaseNettyAction {

    @Inject
    @Reference
    private GroupLogUserRelationService groupLogUserRelationService;

    @Inject
    @Reference
    private GroupLogService groupLogService;

    @Override
    public Result action(Channel income, @Param4Map(fields = {@Param(name = "ids",required = true)}) JSONObject params) {
        ChatUserVO self = NettyCache.globalGet(params.get("appid"));

        JSONArray ids = params.getJSONArray("ids");

        int update = groupLogUserRelationService.update(Chain.make("status", MsgStatusEnum.READ), Cnd.where("groupLogId", "in", ids).and("userId", "=", self.getId()));

        //用户发送消息 提醒 消息发送者 已读
//        for (Object id : ids) {
//            GroupLog groupLog = groupLogService.fetch(id.toString());
//            ChatUserVO from = NettyCache.globalGet(groupLog.getUserId());
//            if(from != null){
//                NettyMsgUtil.send(from.getChannel(), ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.READ, self, groupLog.getId());
//            }
//        }
        return Result.success(update);
    }

}
